package com.hospitalsystem.service.impl;

import com.hospitalsystem.dao.MedicalRecordDao;
import com.hospitalsystem.dao.PatientRecordDao;
import com.hospitalsystem.dao.RefundRecordDao;
import com.hospitalsystem.entity.MedicalRecord;
import com.hospitalsystem.entity.PatientRecord;
import com.hospitalsystem.entity.RefundRecord;
import com.hospitalsystem.service.RefundRecordService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (RefundRecord)表服务实现类
 *
 * @author makejava
 * @since 2024-12-28 19:33:24
 */
@Service("refundRecordService")
public class RefundRecordServiceImpl implements RefundRecordService {
    @Resource
    private RefundRecordDao refundRecordDao;
    @Resource
    private MedicalRecordDao medicalRecordDao;
    @Resource
    private PatientRecordDao patientRecordao;

    private String theTime = "2024-01-03 12:00:00";

    /**
     * 通过ID查询单条数据
     *
     * @param  rRId
     * @return 实例对象
     */
    @Override
    public RefundRecord queryById(String rRId) {
        return this.refundRecordDao.queryById(rRId);
    }

    /**
     * 分页查询
     *
     * @param refundRecord 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RefundRecord> queryByPage(RefundRecord refundRecord, PageRequest pageRequest) {
        long total = this.refundRecordDao.count(refundRecord);
        return new PageImpl<>(this.refundRecordDao.queryAllByLimit(refundRecord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param refundRecord 实例对象
     * @return 实例对象
     */
    @Override
    public RefundRecord insert(RefundRecord refundRecord) {
        this.refundRecordDao.insert(refundRecord);
        return refundRecord;
    }

    /**
     * 修改数据
     *
     * @param refundRecord 实例对象
     * @return 实例对象
     */
    @Override
    public RefundRecord update(RefundRecord refundRecord) {
        this.refundRecordDao.update(refundRecord);
        return this.queryById(refundRecord.getRRId());
    }

    /**
     * 通过主键删除数据
     *
     * @param  rRId
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String rRId) {
        return this.refundRecordDao.deleteById(rRId) > 0;
    }

    @Override
    public RefundRecord addRefundRecord(String time, String mid, String type){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String rrid="01234";
        try{
            //生成本地时间
            Date date = new Date();
            System.out.println("nowtime:  "  + sdf.format(date.getTime()));

            //接卸生成起点时间
            Date start = sdf.parse(theTime);

            //生成基于起点时间的时间戳
            long timestamp = (date.getTime() - start.getTime()) / 10;
            rrid = "RR" +  String.valueOf(timestamp);


        }catch (Exception e){
            e.printStackTrace();
        }

        this.refundRecordDao.addRefundRecord(rrid,time,mid,type);
        return this.refundRecordDao.queryById(rrid);
    }

    @Override
    public List<Map<String,Object>> getRefundRecordsOfOnePatient(String pid){
        List<RefundRecord> refundRecords = this.refundRecordDao.getAllRefundRecords();
        List<Map<String,Object>> refundMaps = new ArrayList<>();

        for(RefundRecord refundRecord:refundRecords){
            Map<String,Object> refundMap = new HashMap<>();

            String mid = refundRecord.getMId();
            MedicalRecord medicalRecord = this.medicalRecordDao.queryById(mid);
            PatientRecord patientRecord = this.patientRecordao.queryById(medicalRecord.getPRId());

            if(patientRecord.getPId().equals(pid)){
                   refundMap.put("time",refundRecord.getTime());
                   refundMap.put("type",refundRecord.getType());
                   List<Map<String,Object>> drugMaps = new ArrayList<>();
                   List<Map<String,Object>> checkMaps = new ArrayList<>();
                   double price = 0.0;
                   if(refundRecord.getType().equals("0")){
                        String[] drugInfos = medicalRecord.getDrugList().split("\n");
                        for(String drugInfo:drugInfos){
                            if(!drugInfo.equals("")){
                                String[] parts = drugInfo.split("\\s");
                                try{
                                    price += this.medicalRecordDao.getDrugByName(parts[0]).getPrice() * Integer.parseInt(parts[1]);
                                    Map<String,Object> drugMap =  new HashMap<>();
                                    drugMap.put(parts[0],parts[1]);
                                    drugMaps.add(drugMap);
                                }catch(Exception e){
                                    e.printStackTrace();
                                }
                            }
                        }
                   }else{
                        String[] checkInfos = medicalRecord.getCheckList().split("\n");
                        for(String checkInfo:checkInfos){
                            if(!checkInfo.equals("")){
                                price += this.medicalRecordDao.getCheckByName(checkInfo).getPrice();
                                Map<String,Object> checkMap = new HashMap<>();
                                checkMap.put(checkInfo,1);
                                checkMaps.add(checkMap);
                            }
                        }
                   }
                   refundMap.put("drugs",drugMaps);
                   refundMap.put("checks",checkMaps);
                   refundMap.put("price",price);
                   refundMap.put("mid",medicalRecord.getMId());
                refundMaps.add(refundMap);
            }
        }
        return refundMaps;

    }
}
